#include "widget.h"
#include "./ui_widget.h"
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QFileDialog>
#include <QDebug>
#include <QDir>
#include <QUrl>
#include <QIcon>
#include <QFile>

Widget::Widget(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    player = new QMediaPlayer;
    playlist = new QMediaPlaylist;
    fileNamesList = new QStringList;

    // 设置应用图标
    this->setWindowIcon(QIcon(":/images/music.png"));

    // 设置样式
    initStyle();

    // 设置信号
    connect(player,&QMediaPlayer::positionChanged,this,&Widget::playerTimeValueChanged);    //刷新歌名和时间
    connect(player, &QMediaPlayer::durationChanged, this, &Widget::updateDuration);         //刷新滑块进度
    connect(ui->playSlider,&QSlider::sliderMoved,this,&Widget::setPosition);                //拖动滑块，改变进度


}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_LocalMusicBtn_clicked()
{
    //qDebug()<<"clicked";
}

void Widget::initStyle()
{
    QFile file;
    file.setFileName(":/css/black.css");
    file.open(QIODevice::ReadOnly);
    QString strQss = file.readAll();
    this->setStyleSheet(strQss);


}

void Widget::on_downloadBtn_clicked()
{
    //qDebug()<<"clicked";
}

void Widget::on_CLoudBtn_clicked()
{
    //qDebug()<<"clicked";
}

void Widget::on_collectBtn_clicked()
{
    //qDebug()<<"clicked";
}

void Widget::on_nextSongBtn_clicked()
{
    //qDebug()<<"clicked";
    if(++currentIndex >= fileNamesList->size())         // 注意这里已经++了
    {
        currentIndex = 0;
    }
    this->ui->musicList->setCurrentRow(this->currentIndex);

    playlist->next();           // 下一首
}

void Widget::on_playOrStopBtn_clicked()
{
    //qDebug()<<"clicked";
    if(this->status==0)     //没有播放，就实现播放
    {
        this->player->play();
        this->status = 1;
    }
    else
    {
        this->player->pause();
        this->status = 0;
    }
}

void Widget::on_preSongBtn_clicked()
{
    //qDebug()<<"clicked";
    if(--currentIndex < 0)         // 注意这里已经--了
    {
        currentIndex = fileNamesList->size()-1;
    }
    this->ui->musicList->setCurrentRow(this->currentIndex);

    playlist->previous();           // 上一首

}

void Widget::on_selectDirBtn_clicked()
{
    path = QFileDialog::getExistingDirectory(this,"请选择本地音乐的文件夹");
    findFile(path);
    if(fileNamesList->empty())
        return;                                 // 如果为空，就直接返回，不执行下面的

    this->ui->musicList->setCurrentRow(this->currentIndex);      // 默认选中第一个
    this->player->setPlaylist(playlist);                         // 放入播放列表
    playlist->setPlaybackMode(QMediaPlaylist::Loop);             // 设置播放模式
}

void Widget::on_musicList_doubleClicked(const QModelIndex &index)
{
    playlist->setCurrentIndex(index.row());
    currentIndex = static_cast<int>(index.row());
    player->play();
}

void Widget::musicPlayer(int)
{

}

// 转换时间数据
static QString formatTime(qint64 timeMilliSeconds)
{
    qint64 seconds = timeMilliSeconds / 1000;
    const qint64 minutes = seconds / 60;
    seconds -= minutes * 60;
    return QStringLiteral("%1:%2")
        .arg(minutes, 2, 10, QLatin1Char('0'))
        .arg(seconds, 2, 10, QLatin1Char('0'));
}

void Widget::playerTimeValueChanged(qint64 value)
{

    ui->labelSongName->setText(player->metaData("Title").toString());
    ui->PlayerTime->setText(formatTime(value) + " / " + formatTime(player->metaData("Duration").toInt()));    // 设置时间
    ui->playSlider->setValue(value);    // 设置滑块最大值
}

void Widget::setPosition(int value)
{
    if (qAbs(player->position() - value) > 99)
        player->setPosition(value);
}

void Widget::findFile(const QString &path)
{
    QDir dir(path);
    *fileNamesList = dir.entryList(QStringList()<<"*.mp3"<<"*.wav");     // 将所有的mp3音乐和wav都放进来
    for(auto musicFileName:*fileNamesList)
    {
        this->playlist->addMedia(QUrl::fromLocalFile(path + "/" + musicFileName));
        this->ui->musicList->addItem(musicFileName);
    }
}

void Widget::updateDuration(qint64 duration)
{
    ui->playSlider->setRange(0,duration);
    ui->playSlider->setEnabled(duration > 0);
    ui->playSlider->setPageStep(duration / 10);
}

